/*
 * CopTool.java
 *
 * Created on __DATE__, __TIME__
 */

package com.gcb.swing.copy.original;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author  __USER__
 */
public class CopyTool extends javax.swing.JFrame implements ActionListener,
		ListSelectionListener {

	/** Creates new form CopTool */
	public CopyTool() {
		initComponents();
		init();
	}
	
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jList1 = new javax.swing.JList();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		jScrollPane2 = new javax.swing.JScrollPane();
		jList2 = new javax.swing.JList();
		jButton3 = new javax.swing.JButton();
		jTextField1 = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jTextField2 = new javax.swing.JTextField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jScrollPane1.setViewportView(jList1);

		jButton1.setText("\u6253\u5f00");
		jButton1.setActionCommand("open");

		jButton2.setText("\u6d4f\u89c8");
		jButton2.setActionCommand("save");

		jLabel1.setText("\u6e90\u6587\u4ef6");

		jScrollPane2.setViewportView(jList2);

		jButton3.setText("\u590d\u5236");
		jButton3.setActionCommand("copy");

		jTextField1.setText("F:\\TestCopy");

		jLabel2.setText("\u76ee\u6807\u8def\u5f84");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(50, 50, 50)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				52,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jTextField2,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				88,
																				Short.MAX_VALUE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				jButton1)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED))
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																214,
																Short.MAX_VALUE))
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				33,
																				33,
																				33)
																		.addComponent(
																				jButton3)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				jScrollPane2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				196,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addContainerGap(
																				86,
																				Short.MAX_VALUE))
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jLabel2)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jTextField1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				148,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButton2)
																		.addGap(
																				41,
																				41,
																				41)))));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				29,
																				29,
																				29)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel2)
																						.addComponent(
																								jButton2)
																						.addComponent(
																								jTextField1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jButton1)
																						.addComponent(
																								jLabel1)
																						.addComponent(
																								jTextField2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGap(
																				50,
																				50,
																				50)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jScrollPane1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jScrollPane2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				163,
																				163,
																				163)
																		.addComponent(
																				jButton3)))
										.addContainerGap(132, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void init() {
		fileListModel1 = new FileListModel();
		fileListModel2 = new FileListModel();
		
		setLocation(300,400);
		jButton1.addActionListener(this);
		jButton2.addActionListener(this);
		jButton3.addActionListener(this);
		jButton3.setEnabled(false);
		jList1.setModel(fileListModel1);
		jList1.addListSelectionListener(this);
		jList2.setModel(fileListModel2);
		jTextField2.setEnabled(false);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CopyTool().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JList jList1;
	private javax.swing.JList jList2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	// End of variables declaration//GEN-END:variables
	private JFileChooser jFileChooser1;
	private FileListModel fileListModel1;
	private FileListModel fileListModel2;

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		boolean success = true;
		String command = e.getActionCommand();
		if ("open".equals(command)) {
			jFileChooser1 = new JFileChooser();
			jFileChooser1.setMultiSelectionEnabled(true);
			jFileChooser1.setCurrentDirectory(new File("F:/"));
			int i = jFileChooser1.showOpenDialog(this);
			if (i == JFileChooser.APPROVE_OPTION) {
				jTextField2.setText(jFileChooser1.getCurrentDirectory().getPath());
				File[] files = jFileChooser1.getSelectedFiles();
				List list = DataHandler.array2List(files);
				fileListModel1.updateData(list);
			}
		} else if ("save".equals(command)) {
			File saveDir = new File("F:/saveDir");
			if(!saveDir.exists())
				saveDir.mkdirs();
			jFileChooser1 = new JFileChooser();
			jFileChooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			jFileChooser1.setCurrentDirectory(saveDir);
			int i = jFileChooser1.showSaveDialog(this);
			if (i == JFileChooser.APPROVE_OPTION) {
				File file = jFileChooser1.getSelectedFile();
				jTextField1.setText(file.getPath());
			}
		} else if ("copy".equals(command)) {
			Object[] objs = jList1.getSelectedValues();
			String txt = jTextField1.getText();
			if ("".equals(txt)) {
				JOptionPane.showMessageDialog(this, "目标路径不能为空", "错误信息",
						JOptionPane.ERROR_MESSAGE);
				return;
			}
			for (int i = 0; i < objs.length; i++) {
				File file = (File) objs[i];
				try {
					DataHandler.copy2(file.getPath(), txt);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					success = false;
					JOptionPane.showMessageDialog(this, "复制错误", "错误信息",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
			}
			if (success) {
				for (int i = 0; i < objs.length; i++) {
					String fileName = ((File) objs[i]).getName();
					objs[i] = fileName;
				}
				List list = DataHandler.array2List(objs);
				fileListModel2.updateData(list);
				String mess = "成功复制" + objs.length + "个文件";
				JOptionPane.showMessageDialog(this, mess, "信息",
						JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		// TODO Auto-generated method stub
		jButton3.setEnabled(!jList1.isSelectionEmpty());
	}

	private class FileListModel extends DefaultListModel {

		private List data = new ArrayList();

		public void updateData(List list) {
			// TODO Auto-generated constructor stub
			data = list;
			super.fireContentsChanged(data, 0, data.size() - 1);
		}

		@Override
		public Object getElementAt(int arg0) {
			// TODO Auto-generated method stub
			return data.get(arg0);
		}

		@Override
		public int getSize() {
			// TODO Auto-generated method stub
			return data.size();
		}

		public List getData() {
			return data;
		}

		public void setData(List data) {
			this.data = data;
		}

	}

}